Ext.onReady(function(){
    Ext.define('LettersReceiveModel',{
        extend: 'Ext.data.Model',
        fields: [
			{name:'id',type:'string'},
            {name:'sender__name',type:'string'},
            {name:'send_time',type:'string'},
            {name:'title',type:'string'},
            {name:'content',type: 'string'}
        ]
    });
    //创建数据源
    var LettersReceiveStore = Ext.create('Ext.data.Store', {
    	pageSize:20,
        model: 'LettersReceiveModel',
	    proxy: {
	        type: 'ajax',
	        url: '/resourcecontrol/letters_receive_list/',
	        actionMethods:'post',
	        reader: {
	            type: 'json',
	            root: 'rows',
	            totalProperty: 'totalCount'
	        }
	    },
	    autoLoad: true
    });
    //创建Grid
    var LettersReceiveGrid = Ext.create('Ext.grid.Panel',{
    	title:'收件箱',
        store: LettersReceiveStore,
        columnLines:true,
        columns: [
            Ext.create('Ext.grid.RowNumberer'),// 序列     
            {text: "发送人", width: 100, dataIndex: 'sender__name', sortable: false},
            {text: "发送时间", width: 160, dataIndex: 'send_time',sortable:false},
            {text: "主题", width: 300,flex:1, dataIndex: 'title', sortable: false}
            //{text: "内容", width: 120,flex:1, dataIndex: 'content', sortable: false}
        ],
        layout : 'fit',
        loadMask:true,
        viewConfig: {
            stripeRows: true,
            listeners: {
                itemclick: function(dataview, record, item, index, e) {       
                	LettersForm.enable();
                }
            }                
        },
        dockedItems : [{
            xtype : 'pagingtoolbar',
            store : LettersReceiveStore,
            dock : 'bottom',
            displayInfo : true,
            autoScroll:true 
        },{
        	dock:'top',
        	xtype:'toolbar',
        	items:[{
				xtype:'datefield',
				fieldLabel: '日期',
				labelWidth: 40,
				id:'startdate',
				editable:false,
				format: 'Y-m-d'
			},'——',{
				xtype:'datefield',
				labelWidth: 40,
				id:'enddate',
				editable:false,
				format: 'Y-m-d'
			},{
				text:'查询',
				iconCls:'search', 
				handler:function(){
					params={startdate:Ext.util.Format.dateRenderer('Y-m-d')(Ext.getCmp('startdate').getValue()),
							enddate:Ext.util.Format.dateRenderer('Y-m-d')(Ext.getCmp('enddate').getValue())}
					Ext.apply(LettersReceiveStore.proxy.extraParams, params);
					LettersReceiveStore.currentPage=1;
					LettersReceiveStore.load();
				}
			}]
        }]
    });
    
   
    var LettersForm = Ext.create('Ext.form.Panel', {
    	title:'详情',
    	disabled:true,
        plain: true,
        border: 0,
        bodyPadding: 5,
        fieldDefaults: {
        	labelAlign: 'right',
            labelWidth: 60,
            anchor: '100%'
        },
        layout: {
            type: 'vbox',
            align: 'stretch'
        },
        items: [{
			xtype:'displayfield',
			fieldLabel: '主题',
			name:'title',
			allowBlank: false
		},{
            xtype: 'displayfield',
            fieldLabel: '发送人',
            readOnly: true,
            name: 'sender__name',
            allowBlank: false
        },{
            xtype: 'displayfield',
            fieldLabel: '发送时间',
            name: 'send_time'
        },{
            xtype: 'displayfield',
            fieldLabel: '正文',
            name: 'content',
            flex:1
        },{
            xtype: 'fieldcontainer',
            id:'attachmentContainer',
            height: 60,
            autoScroll: true,
            layout: {
                type: 'anchor'
            },
            anchor: '100%',
            fieldLabel: '附件'
        }]
    });
	
	
	Ext.create('Ext.container.Viewport',{
		layout:'fit',
		renderTo: 'lettersreceive-grid',
		//items:[LettersReceiveGrid]
		items:[Ext.create('Ext.tab.Panel',{
			activeTab: 0,
			items:[LettersReceiveGrid,LettersForm],
			listeners:{
				'tabchange':function(tabPanel, newCard,oldCard,eOpts){
					if(newCard==LettersForm){
						LettersForm.getForm().reset();
						Ext.getCmp('attachmentContainer').removeAll();
						var selected = LettersReceiveGrid.getSelectionModel().getSelection();
						LettersForm.loadRecord(selected[0]);
						Ext.Ajax.request({
	        			    url:'/resourcecontrol/letters_attachment_query/',
	        			    method : 'post',
	        			    params:{
	        			    	lettersId:selected[0].data.id
	        			    },
	        			    success:function(response){
	        			        var results =  Ext.decode(response.responseText);
	        			        if(results.success){
	        			        	for(var i=0;i<results.data.length;i++){
					            		var ChildContainer=Ext.create('Ext.container.Container',{
					            			id:results.data[i].path,
					            			name:results.data[i].name,
								            items: [{
							                    xtype: 'label',
							                    text: results.data[i].name
							                },{
							                    xtype: 'button',
							                    text: '下载',
							                    handler:function(){
							                    	window.open('/downloaded/?path='+this.findParentByType('container').id+'&name='+this.findParentByType('container').name);
							                    }
							            	}]		            			
					            		});	
					            		Ext.getCmp('attachmentContainer').add(ChildContainer);
	        			        	}
	        			        }else{
	        			        	Ext.Msg.alert('系统提示', results.message);
	        			        }
	        			    }
	        			});
					}else if(newCard==LettersReceiveGrid){
						oldCard.disable();
						var selected = LettersReceiveGrid.getSelectionModel().getSelection();
						Ext.Ajax.request({
	        			    url:'/resourcecontrol/letters_lookup_save/',
	        			    method : 'post',
	        			    params:{
	        			    	lettersId:selected[0].data.id
	        			    },
	        			    success:function(response){
	        			        var results =  Ext.decode(response.responseText);
	        			        if(results.success){
	        			        	LettersReceiveStore.load();
	        			        }else{
	        			        	Ext.Msg.alert('系统提示', results.message);
	        			        }
	        			    }
	        			});						
					}
				}		
			}
		})]
	});	
});